package cc.mrbird.febs.statistics.controller;

import cc.mrbird.febs.common.utils.FebsUtil;
import cc.mrbird.febs.statistics.dto.QueryParam;
import cc.mrbird.febs.accounting.service.IStandardSubService;
import cc.mrbird.febs.common.controller.BaseController;
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.statistics.service.IOverviewService;
import cc.mrbird.febs.utils.ExportUtil;
import cc.mrbird.febs.utils.PagingDTO;
import cc.mrbird.febs.utils.PermissionUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;


@Slf4j
@RestController
@RequestMapping(value = "/overview")
public class OverviewController extends BaseController {

    @Resource
    private IStandardSubService standardSubService;
    @Resource
    private IOverviewService overviewService;

    @RequestMapping(value = "/getStatisticsStandard", method = RequestMethod.POST)
    public FebsResponse getStatisticsStandard() {
        Integer permissionId= PermissionUtil.perMap.get(Integer.parseInt(FebsUtil.getCurrentUser().getRoleId()));
        return new FebsResponse().success().data(standardSubService.getStatisticsStandard());
    }

    @RequestMapping(value = "/overviewTable", method = RequestMethod.GET)
    public FebsResponse overviewTable(QueryRequest request, QueryParam queryParam) {
        if (Objects.isNull(queryParam.getYear())) {
            List<JSONObject> res = new ArrayList<>();
            return new FebsResponse().success().data(new PagingDTO(1, 1, 0L, res));
        } else {
            return new FebsResponse().success().data(overviewService.overviewTable(request, queryParam));
        }
    }

    @RequestMapping(value = "/detailTable", method = RequestMethod.GET)
    public FebsResponse detailTable(QueryRequest request, QueryParam queryParam) {
        if (Objects.isNull(queryParam.getYear())) {
            List<JSONObject> res = new ArrayList<>();
            return new FebsResponse().success().data(new PagingDTO(1, 1, 0L, res));
        } else {
            return new FebsResponse().success().data(overviewService.detailTable(request, queryParam));
        }
    }

    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public void export(HttpServletResponse response, @RequestParam("dataStr") String dataStr, @RequestParam("cols") String cols,
                       @RequestParam("statisticalMethod") String statisticalMethod, @RequestParam("ifShowMonth") String ifShowMonth, @RequestParam("month") String month) {
        List<String> months = new ArrayList<>(Arrays.asList(month.split(",")));
        List<String> pMonths = new ArrayList<>();
        for (int i = 1; i <= 12; i++) {
            if (months.contains(i + "")) {
                pMonths.add(i + "");
            }
        }
        List<JSONObject> data = JSONArray.parseArray(dataStr, JSONObject.class);
        List<String> titles = new ArrayList<>();
        titles.add("序号");
        titles.addAll(Arrays.asList(cols.split(",")));
        List<String> keys = new ArrayList<>();
        keys.add("key");
        String sheetName = statisticalMethod.equals("1") ? "总览统计" : "明细统计";
        if (statisticalMethod.equals("1")) {
            keys.add("title");
            for (int i = 0; i < titles.size() - 1; i++) {
                keys.add("col" + (i + 1 + ""));
                if (ifShowMonth.equals("1")) {
                    for (int j = 0; j < months.size(); j++) {
                        keys.add("mon" + (i * months.size() + j + 1));
                    }
                }
            }
        } else {
            keys.add("subName");
            keys.add("cost");
            if (ifShowMonth.equals("1")) {
                keys.addAll(pMonths);
            }
        }
        Workbook workbook = ExportUtil.simpleExport(sheetName, data, titles, keys);
        try {
            OutputStream output;
            output = response.getOutputStream();
            BufferedOutputStream bufferOutput = new BufferedOutputStream(output);
            bufferOutput.flush();
            workbook.write(bufferOutput);
            bufferOutput.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
